package com.lanhai.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * 字符串操作工具类
 * @author lm
 * @since 1.0
 */
public class StringUtil {

	/*
	 * 打印出异常
	 */
	public static String getTrace(Throwable t) {
		StringWriter stringWriter = new StringWriter();
		PrintWriter writer = new PrintWriter(stringWriter);
		t.printStackTrace(writer);
		StringBuffer buffer = stringWriter.getBuffer();
		return buffer.toString();
	}

	/**
	 * @param urlparam 带分隔的url参数
	 * @return
	 */
	public static Map<String,String> Split(String urlparam){
	   Map<String,String> map = new HashMap<String,String>();
	   String[] param =  urlparam.split("&");
	   for(String keyvalue:param){
	      String[] pair = keyvalue.split("=");
	      if(pair.length==2){
	         map.put(pair[0], pair[1]);
	      }
	   }
	   return map;
	}
	
	/**
	 * 判断字符串是否非空
	 */
	public static boolean isNotEmpty(String str) {
		return StringUtils.isNotBlank(str);
	}
    /**
     * 判断字符串是否为空
     */
    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }
    
    /**
     * 将字符串转换成Double数组
     */
    public static Double[] convertDoubleArray(String str){
		String[] numArr = str.split(",");
		Double[] dArray = new Double[numArr.length];
		if(numArr.length > 0){
			for(int i = 0; i < numArr.length; i++){
				if(!numArr[i].equals("")){
					dArray[i] = Double.parseDouble(numArr[i]);
				}
			}
		}
		return dArray;
    }
}
